/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package RestaurantManagement.oop.demo.restaurantmanagementsystem;
/**
 * Restaurant billing implementation that handles
 * customer orders and bill calculation
 * @author ashongtical
 */

import java.util.ArrayList;
import java.util.HashMap;
public class RestaurantBilling extends RestaurantBillingSystem {
    private ArrayList<Meal> order;
    private HashMap<String, Integer>mealQuantities;
    private HashMap<String, ArrayList<Meal>> orderMap;

    //Constructor to initialize order list and meal quantities map
    public RestaurantBilling() {
        super();
        this.order = new ArrayList<>();
        this.mealQuantities = new HashMap<>();
    }

    //Add a meal from the menu to the customer's order
    //mealName: The name of the meal to add
    //return true if meal was added successfully, false otherwise
    @Override
    public boolean addMealToOrder(String mealName){
        Meal meal = findMealByName(mealName);
        if(meal != null){
            order.add(meal);
            mealQuantities.put(mealName, mealQuantities.getOrDefault(mealName, 0)+1);
            System.out.println("Added '"+mealName+"' to order");
            return true;
        }
        System.out.println("Meal '"+mealName+"' not found in menu");
        return false;
    }

    //Override the calculateBill() and calculate the total bill for the customer's order
    //return the total bill amount
    @Override
    public double calculateBill(){
        double total = 0;
        for(Meal meal : order){
            total += meal.getPrice();
        }
        return total;
    }

    //Clear the current order
    public void clearOrder(){
        order.clear();
        mealQuantities.clear();
        System.out.println("Order cleared");
    }

    //Display the current order with meal quantities and total price
    public void displayOrder(){
        if(order.isEmpty()){
            System.out.println("No items in current order.");
            return;
        }
        System.out.println("\n==== CURRENT ORDER ====");
        for (String mealName : mealQuantities.keySet()) {
            Meal meal = findMealByName(mealName);
            int quantity = mealQuantities.get(mealName);
            System.out.println(String.format("%-20s x%d  RMB%.2f", mealName, quantity, meal.getPrice() * quantity));
        }
        System.out.println("--------------------------");
        System.out.println(String.format("TOTAL: RMB%.2f", calculateBill()));
        System.out.println("==========================");
    }

    //Generate a formatted bill for the customer
    //customerName: The name of the customer
    //return String representation of the bill
    public String generateBill(String customerName){
        StringBuilder bill = new StringBuilder();
        bill.append("\n===== RESTAURANT BILL =====\n");
        bill.append("Customer: ").append(customerName).append("\n");
        bill.append("--------------------------\n");

        for (String mealName : mealQuantities.keySet()) {
            Meal meal = findMealByName(mealName);
            int quantity = mealQuantities.get(mealName);
            bill.append(String.format("%-20s x%d  RMB%.2f\n", mealName, quantity, meal.getPrice() * quantity));
        }

        bill.append("--------------------------\n");
        bill.append(String.format("TOTAL: RMB%.2f\n", calculateBill()));
        bill.append("==========================\n");
        bill.append("Thank you for dining with us!\n");

        return bill.toString();

    }
}
